Permutations

Medium

Extra practice. This problem has no walkthrough slides. Try solving it with the pattern template on your own, and lean on the hints if you get stuck.

Question

Given a list of distinct integers, build every possible arrangement of all the values in the list. Two arrangements count as different whenever the values appear in a different sequence.

Return every arrangement you find. The order in which you list the arrangements does not matter, but the order of the values within each arrangement is what makes it unique.

Input: nums = [1, 2]

Output: [[1, 2], [2, 1]]

With 2 values, there are 2 ways to line them up.

Input: nums = [4, 6, 9]

Output: [[4, 6, 9], [4, 9, 6], [6, 4, 9], [6, 9, 4], [9, 4, 6], [9, 6, 4]]

With 3 values, there are 6 ways to line them up: pick which value goes first, then which of the remaining two goes second.

Input: nums = []

Output: [[]]

An empty list has exactly one arrangement: the empty one.

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

How many arrangements should be returned for nums = [3, 6, 9]?
3
6
9
27

Take a moment to understand the problem and think of your approach before you start coding.